package com.py.framework.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.framework.common.Constants;
import com.py.framework.common.controller.SystemController;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.helper.HttpServletHelper;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.entity.RoleEntity;
import com.py.framework.entity.UserEntity;
import com.py.framework.service.AuthenticationService;
import com.py.framework.service.RoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 角色管理Controller
 * @author Leegern
 * @date   2018年4月26日
 */
@Api(tags = "role", description = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController extends SystemController {
	
	public static final String PERMISSION_DENIED = "你无此权限，不能使用此功能，请联系管理员";

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private AuthenticationService authenticationService;

	/**
	 * @api {post} /role/addRoleInfo.json 添加角色实体
	 * @apiName addRoleInfo
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {RoleEntity} entity 角色实体
	 * @apiParam {String} roleName 角色名称
	 * @apiParam {String} roleDesc 角色描述
	 * @apiParam {int} roleType 角色类型，1：超级管理员，2：管理员，3：普通
	 * @apiParam {int} roleStatus 角色状态，1：开启，2：停用
	 * @apiParamExample {json} 请求参数示例
	 *  {"createTime":1531362496000,"updateTime":1531362496000,"roleName":"超级管理员","roleDesc":"超级管理员","roleType":1,"roleStatus":2}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/addRoleInfo", method = RequestMethod.POST)
	@ApiOperation(value = "添加角色", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "RoleEntity", name = "entity", value = "角色信息实体", required = true) })
	public RestServiceResult<Object> addRoleInfo(@RequestBody RoleEntity entity, HttpServletRequest request) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (null != entity) {
			try {
				// 获取当前用户信息
				UserEntity user = authenticationService.getCurrentUserInfo(HttpServletHelper.getUserToken(request));
				if (null != user && user.getId() > Constants.NUMBER_ZERO && 3 != user.getUserType()) {
					// 只有超级管理员可见
					return super.getFailedRestServiceResult(PERMISSION_DENIED);
				}
				entity.setUserId(user == null ? 1 : user.getId());
				roleService.addRoleInfo(entity);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("添加失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/queryRolesForPage.json 分页查询角色列表
	 * @apiName queryRolesForPage
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {int} page  当前页码
	 * @apiParam {int} limit 每页条数
	 * @apiParam {String} roleName 角色名称
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {long} id 角色Id
	 * @apiSuccess {String} roleName 角色名称
	 * @apiSuccess {String} roleDesc 角色描述
	 * @apiSuccess {int} roleType 角色类型，1：超级管理员，2：管理员，3：普通
	 * @apiSuccess {int} roleStatus 角色状态，1：开启，2：停用
	 * @apiSuccess {long} officeId 部门Id
	 * @apiSuccess {long} menuId 资源Id
	 * @apiSuccess {String} createTime 创建时间
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":3,"data":[{"id":1,"createTime":1531362496000,"updateTime":1531362496000,"roleName":"超级管理员","roleDesc":"超级管理员","roleType":1,"roleStatus":2}]}
	 */
	@RequestMapping(value = "/queryRolesForPage", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询角色列表", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "int", name = "page", value = "页码", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "int", name = "limit", value = "分页大小", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "roleName", value = "角色名称", required = true) })
	public RestServiceResult<List<RoleEntity>> queryRolesForPage(int page, int limit, String roleName,
			HttpServletRequest request) {
		Map<String, Object> params = new HashMap<>();
		Page pager = super.getPage(page, limit);
		params.put("page", pager);
		params.put("roleName", roleName);
		try {
			// 获取当前用户信息
			UserEntity user = authenticationService.getCurrentUserInfo(HttpServletHelper.getUserToken(request));
			if (!user.getRoleName().equals("系统管理员")) {
				return super.getPageSuccessRestServiceResult(pager.getTotalCount(), Collections.emptyList());
			}
		} catch (Exception e) {
			ExceptionLogger.error("RoleController#queryRolesForPage", "获取当前用户数据异常", e);
			return super.getPageSuccessRestServiceResult(pager.getTotalCount(), Collections.emptyList());
		}
		List<RoleEntity> datas = roleService.queryRolesForPage(params);
		return super.getPageSuccessRestServiceResult(pager.getTotalCount(), datas);
	}
	
	/**
	 * @api {post} /role/getAllRoles.json 获取所有角色信息
	 * @apiName getAllRoles
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {long} id 角色Id
	 * @apiSuccess {String} roleName 角色名称
	 * @apiSuccess {String} roleDesc 角色描述
	 * @apiSuccess {int} roleType 角色类型，1：超级管理员，2：管理员，3：普通
	 * @apiSuccess {int} roleStatus 角色状态，1：开启，2：停用
	 * @apiSuccess {long} officeId 部门Id
	 * @apiSuccess {long} menuId 资源Id
	 * @apiSuccess {String} createTime 创建时间
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":3,"data":[{"id":1,"createTime":1531362496000,"updateTime":1531362496000,"roleName":"超级管理员","roleDesc":"超级管理员","roleType":1,"roleStatus":2}]}
	 */
	@RequestMapping(value = "/getAllRoles", method = RequestMethod.POST)
	@ApiOperation(value = "获取所有角色信息", httpMethod = "POST", produces = "application/json")
	public RestServiceResult<List<RoleEntity>> getAllRoles() {
		List<RoleEntity> datas = roleService.getAllRoles();
		return super.getSuccessRestServiceResult(datas);
	}
	
	/**
	 * @api {post} /role/findOneRole.json 根据角色Id获取角色信息
	 * @apiName findOneRole
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {long} id 角色Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {long} id 角色Id
	 * @apiSuccess {String} roleName 角色名称
	 * @apiSuccess {String} roleDesc 角色描述
	 * @apiSuccess {int} roleType 角色类型，1：超级管理员，2：管理员，3：普通
	 * @apiSuccess {int} roleStatus 角色状态，1：开启，2：停用
	 * @apiSuccess {long} officeId 部门Id
	 * @apiSuccess {long} menuId 资源Id
	 * @apiSuccess {String} createTime 创建时间
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":1,"data":{"id":1,"createTime":1531362496000,"updateTime":1531362496000,"roleName":"超级管理员","roleDesc":"超级管理员","roleType":1,"roleStatus":2}}
	 */
	@RequestMapping(value = "/findOneRole", method = RequestMethod.POST)
	@ApiOperation(value = "根据角色Id获取角色信息", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "long", name = "id", value = "角色ID", required = true) })
	public RestServiceResult<RoleEntity> findOneRole(long id) {
		RoleEntity data = null;
		if (id > Constants.NUMBER_ZERO) {
			data = roleService.findOneRole(id);
		}
		return super.getSuccessRestServiceResult(data);
	}
	
	/**
	 * @api {post} /role/getRoleUserRelate.json 根据角色Id获取角色用户关联数据
	 * @apiName getRoleUserRelate
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {long} roleId 角色Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {long} id 角色Id
	 * @apiSuccess {String} roleName 角色名称
	 * @apiSuccess {long} userId 用户Id
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":3,"data":[{"id":1,"roleName":"超级管理员","userId":2}]}
	 */
	@RequestMapping(value = "/getRoleUserRelate", method = RequestMethod.POST)
	@ApiOperation(value = "根据角色Id获取角色用户关联数据", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "long", name = "roleId", value = "角色ID", required = true) })
	public RestServiceResult<List<RoleEntity>> getRoleUserRelate(long roleId) {
		List<RoleEntity> datas = null;
		if (roleId > Constants.NUMBER_ZERO) {
			datas = roleService.getRoleUserRelate(roleId);
		}
		return super.getSuccessRestServiceResult(datas);
	}
	
	/**
	 * @api {post} /role/getRoleMenuRelate.json 根据角色Id获取角色资源关联数据
	 * @apiName getRoleMenuRelate
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {long} roleId 角色Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {long} id 角色Id
	 * @apiSuccess {String} roleName 角色名称
	 * @apiSuccess {long} menuId 菜单Id
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":1,"data":[{"id":1,"roleName":"超级管理员","menuId":2}]}
	 */
	@RequestMapping(value = "/getRoleMenuRelate", method = RequestMethod.POST)
	@ApiOperation(value = "根据角色Id获取角色资源关联数据", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "long", name = "roleId", value = "角色ID", required = true) })
	public RestServiceResult<List<RoleEntity>> getRoleMenuRelate(long roleId) {
		List<RoleEntity> datas = null;
		if (roleId > Constants.NUMBER_ZERO) {
			datas = roleService.getRoleMenuRelate(roleId);
		}
		return super.getSuccessRestServiceResult(datas);
	}
	
	/**
	 * @api {post} /role/getRolesByUserId.json  根据用户Id获取角色用户关联数据
	 * @apiName getRolesByUserId
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {long}  userId 用户Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {long} id 角色Id
	 * @apiSuccess {String} roleName 角色名称
	 * @apiSuccess {long} userId 用户Id
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":3,"data":[{"id":1,"roleName":"超级管理员","userId":2}]}
	 */
	@RequestMapping(value = "/getRolesByUserId", method = RequestMethod.POST)
	@ApiOperation(value = "根据用户Id获取角色用户关联数据", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "long", name = "roleId", value = "角色ID", required = true) })
	public RestServiceResult<List<RoleEntity>> getRolesByUserId(long userId) {
		List<RoleEntity> datas = null;
		if (userId > Constants.NUMBER_ZERO) {
			datas = roleService.getRolesByUserId(userId);
		}
		return super.getSuccessRestServiceResult(datas);
	}
	
	/**
	 * @api {post} /role/updateRoleInfo.json 更新角色信息
	 * @apiName updateRoleInfo
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {RoleEntity} entity
	 * @apiParam {long} id 角色Id
	 * @apiParam {String} roleName 角色名称
	 * @apiParam {String} roleDesc 角色描述
	 * @apiParam {int} roleType 角色类型，1：超级管理员，2：管理员，3：普通
	 * @apiParam {int} roleStatus 角色状态，1：开启，2：停用
	 * @apiParamExample {json} 请求参数示例
	 *  {"createTime":1531362496000,"updateTime":1531362496000,"roleName":"超级管理员","roleDesc":"超级管理员","roleType":1,"roleStatus":2}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/updateRoleInfo", method = RequestMethod.POST)
	@ApiOperation(value = "根据用户Id获取角色用户关联数据", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "RoleEntity", name = "entity", value = "角色信息实体", required = true) })
	public RestServiceResult<Object> updateRoleInfo(@RequestBody RoleEntity entity) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (null != entity) {
			try {
				roleService.updateRoleInfo(entity);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("更新失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/batchDelRoles.json 批量删除角色
	 * @apiName batchDelRoles
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {String}  ids 角色Id集合
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/batchDelRoles", method = RequestMethod.POST)
	@ApiOperation(value = "批量删除角色", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "ids", value = "角色Id集合", required = true) })
	public RestServiceResult<Object> batchDelRoles(String ids) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (StringUtils.isNotBlank(ids)) {
			try {
				roleService.batchDelRoles(super.convert2Long(ids));
			} catch (ServiceException e) {
				result = super.getFailedRestServiceResult(e.getMessage());
				ExceptionLogger.error(e);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("删除失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/addRoleUserRelate.json 添加角色用户信息集合
	 * @apiName addRoleUserRelate
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {RoleEntity} datas 数据集合
	 * @apiParam {long} id 角色Id
	 * @apiParam {long} userId 用户Id
	 * @apiParamExample {json} 请求参数示例
	 *  [{"id": 1000, "userId": 10}]
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/addRoleUserRelate", method = RequestMethod.POST)
	@ApiOperation(value = "添加角色用户信息集合", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "List", name = "datas", value = "数据集合", required = true) })
	public RestServiceResult<Object> addRoleUserRelate(@RequestBody List<RoleEntity> datas,
			HttpServletRequest request) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (! CollectionUtils.isEmpty(datas)) {
			try {
				UserEntity user = authenticationService.getCurrentUserInfo(HttpServletHelper.getUserToken(request));
				if (null != user && user.getId() > Constants.NUMBER_ZERO && 1 == user.getUserType()) {
					// 普通用户无法创建用户
					return super.getFailedRestServiceResult(PERMISSION_DENIED);
				}
				roleService.addRoleUserRelate(datas);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("添加失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/modifyRoleUserRelate.json 修改角色用户信息
	 * @apiName modifyRoleUserRelate
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {RoleEntity} datas 数据集合
	 * @apiParam {long} id 角色Id
	 * @apiParam {long} userId 用户Id
	 * @apiParamExample {json} 请求参数示例
	 *  [{"id": 1000, "userId": 10}]
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/modifyRoleUserRelate", method = RequestMethod.POST)
	@ApiOperation(value = "修改角色用户信息", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "List", name = "datas", value = "数据集合", required = true) })
	public RestServiceResult<Object> modifyRoleUserRelate(@RequestBody List<RoleEntity> datas,
			HttpServletRequest request) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (! CollectionUtils.isEmpty(datas)) {
			try {
				UserEntity user = authenticationService.getCurrentUserInfo(HttpServletHelper.getUserToken(request));
				if (null != user && user.getId() > Constants.NUMBER_ZERO && 1 == user.getUserType()) {
					// 普通用户无法创建用户
					return super.getFailedRestServiceResult(PERMISSION_DENIED);
				}
				roleService.modifyRoleUserRelate(datas);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("修改失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/addRoleMenuRelate.json 添加角色菜单关联数据集合
	 * @apiName addRoleMenuRelate
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {RoleEntity} datas 数据集合
	 * @apiParam {long} id 角色Id
	 * @apiParam {long} menuId 资源Id
	 * @apiParamExample {json} 请求参数示例
	 *  [{"id": 1000, "menuId": 10}]
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/addRoleMenuRelate", method = RequestMethod.POST)
	@ApiOperation(value = "添加角色菜单关联数据集合", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "List", name = "datas", value = "数据集合", required = true) })
	public RestServiceResult<Object> addRoleMenuRelate(@RequestBody List<RoleEntity> datas) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (! CollectionUtils.isEmpty(datas)) {
			try {
				roleService.addRoleMenuRelate(datas);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("添加失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/modifyRoleMenuRelate.json 修改角色菜单信息
	 * @apiName modifyRoleMenuRelate
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {RoleEntity} datas 数据集合
	 * @apiParam {long} id 角色Id
	 * @apiParam {long} menuId 资源Id
	 * @apiParamExample {json} 请求参数示例
	 *  [{"id": 1000, "menuId": 10}]
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/modifyRoleMenuRelate", method = RequestMethod.POST)
	@ApiOperation(value = "修改角色菜单信息", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "List", name = "datas", value = "数据集合", required = true) })
	public RestServiceResult<Object> modifyRoleMenuRelate(@RequestBody List<RoleEntity> datas) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (! CollectionUtils.isEmpty(datas)) {
			try {
				roleService.modifyRoleMenuRelate(datas);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("修改失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/addRoleOfficeRelate.json 添加角色部门关联数据集合
	 * @apiName addRoleOfficeRelate
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {RoleEntity} datas 数据集合
	 * @apiParamExample {json} 请求参数示例
	 *  [{"id": 1000, "officeId": 10}]
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *    {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/addRoleOfficeRelate", method = RequestMethod.POST)
	@ApiOperation(value = "添加角色部门关联数据集合", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "List", name = "datas", value = "数据集合", required = true) })
	public RestServiceResult<Object> addRoleOfficeRelate(@RequestBody List<RoleEntity> datas) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (! CollectionUtils.isEmpty(datas)) {
			try {
				roleService.addRoleOfficeRelate(datas);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("添加失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/modifyRoleOfficeRelate.json 修改角色部门信息
	 * @apiName modifyRoleOfficeRelate
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {RoleEntity} datas 数据集合
	 * @apiParam {long} id 角色Id
	 * @apiParam {long} officeId 部门Id
	 * @apiParamExample {json} 请求参数示例
	 *  [{"id": 1000, "officeId": 10}]
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/modifyRoleOfficeRelate", method = RequestMethod.POST)
	@ApiOperation(value = "修改角色部门信息", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "List", name = "datas", value = "数据集合", required = true) })
	public RestServiceResult<Object> modifyRoleOfficeRelate(@RequestBody List<RoleEntity> datas) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (! CollectionUtils.isEmpty(datas)) {
			try {
				roleService.modifyRoleOfficeRelate(datas);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("修改失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /role/isExistRole.json 根据角色名称判断是否有重复数据
	 * @apiName isExistRole
	 * @apiVersion 0.0.1
	 * @apiGroup 角色管理 
	 * @apiParam {String} roleName 角色名称
	 * @apiParam {long} roleId 角色Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} true 存在重复, false 不存在重复
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":{"flag":"true"}}
	 */
	@RequestMapping(value = "/isExistRole", method = RequestMethod.POST)
	@ApiOperation(value = "根据角色名称判断是否有重复数据", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "roleName", value = "角色名称", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "long", name = "roleId", value = "角色Id", required = true) })
	public RestServiceResult<Map<String, String>> isExistRole(String roleName,
			@RequestParam(value="roleId", required=false, defaultValue="0") long roleId) {
		RestServiceResult<Map<String, String>> rstData = null;
		Map<String, String> result = new HashMap<>();
		String flag = "";
		if (StringUtils.isNotBlank(roleName)) {
			try {
				List<RoleEntity> datas = roleService.getRolesByName(roleId, roleName);
				if (! CollectionUtils.isEmpty(datas)) {
					flag = "true";
				} else {
					flag = "false";
				}
				result.put("flag", flag);
			} catch (Exception e) {
				rstData = super.getFailedRestServiceResult("查询失败！");
				ExceptionLogger.error(e);
			}
		}
		rstData = super.getSuccessRestServiceResult(result);
		return rstData;
	}
}